﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;

namespace Fonlow.BetterRobocopyGui
{
    internal class TextEditorWindow : IDisposable
    {
        public Form Window { get; private set; }
        public RichTextBox TextBox { get { return textBox; } }

        RichTextBox textBox;

        public TextEditorWindow()
        {
            Window = new Form();
            textBox = new RichTextBox(); // RichText is good for checking the cursor position through the SelectionChanged event.
            textBox.Dock = DockStyle.Fill;
            textBox.Multiline = false;
            textBox.WordWrap = true;
            textBox.Font = new Font("Courier New", 10, FontStyle.Regular);
            textBox.DetectUrls = false;


            Window.Text = "Command";
            Window.ShowInTaskbar = false;
            Window.Controls.Add(textBox);

            textBox.SelectionChanged += new EventHandler(textBox_SelectionChanged);
        }

        /// <summary>
        /// Telling the world what parameter selected through the WordSelected event being fired.
        /// </summary>
        void textBox_SelectionChanged(object sender, EventArgs e)
        {
            int slashPosition = -1;
            int selectionStart = textBox.SelectionStart;
            if (selectionStart == textBox.Text.Length)
                selectionStart--;

            for (int i = selectionStart; i > 0; i--)
            {
                if (textBox.Text[i] == '/')
                {
                    slashPosition = i;
                    break;
                }
            }

            if (slashPosition > -1)
            {
                Regex parametersRegex = new Regex(Properties.Resources.PARAMETERS, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
                Match m = parametersRegex.Match(textBox.Text.Substring(slashPosition));
                if (m.Success)
                {
                    if (m.Groups["Name"].Success)
                    {
                        string parameterName = m.Groups["Name"].Value;
                        if (WordSelected != null)
                        {
                            WordSelected(this, new StringEventArgs(parameterName));
                            System.Diagnostics.Debug.WriteLine(parameterName);
                        }
                    }
                }

            }

        }

        /// <summary>
        /// Fired when mouse clicking anywhere in the commandline, with / in front.
        /// </summary>
        public event EventHandler<StringEventArgs> WordSelected;

        #region IDisposable Members
        bool disposed;

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    Window.Dispose();
                }
                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }

    internal class OutputWindow : IDisposable
    {
        public OutputWindow()
        {
            Window = new Form();
            textBox = new TextBox();
            textBox.Dock = DockStyle.Fill;
            textBox.Multiline = true;
            textBox.WordWrap = true;
            textBox.ScrollBars = ScrollBars.Vertical;
            textBox.Font = new Font("Courier New", 9, FontStyle.Regular);

            Window.Text = "Output";
            Window.ShowInTaskbar = false;
            Window.Controls.Add(textBox);
        }

        public Form Window { get; private set; }
        public TextBox TextBox { get { return textBox; } }

        TextBox textBox;

        #region IDisposable Members
        bool disposed;

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    Window.Dispose();
                }
                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
